package manager
{
	import core.layer.XMouseHandLayer;
	
	import def.XG;
	
	import fl.containers.UILoader;
	
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;

	public class XMouseManager
	{
		static public const FUNTYPE_MOUSEMOVE : String = 		"FUNTYPE_MOUSEMOVE";
		static public const FUNTYPE_MOUSECLICK : String = 	"FUNTYPE_MOUSECLICK";
		static public const FUNTYPE_MOUSEDOWN : String = 		"FUNTYPE_MOUSEDOWN";
		static public const FUNTYPE_MOUSEUP : String = 		"FUNTYPE_MOUSEUP";
		
		public var handBox : Sprite;
		public var loadHandBox : Loader;
		public var group : Array;
		
		private var mouseMoveFunGroup : Array;
		private var mouseClickFunGroup : Array;
		private var mouseDownFunGroup : Array;
		private var mouseUpFunGroup : Array;
		
		private var m_handBoxOffX : Number;
		private var m_handBoxOffY : Number;
		
		public var box : Sprite;
		
		public function XMouseManager()
		{
			loadHandBox = new Loader;
			handBox = new Sprite;
			group = [];
			
			mouseMoveFunGroup = [];
			mouseClickFunGroup = [];
			mouseDownFunGroup = [];
			mouseUpFunGroup = [];
			
			m_handBoxOffX = 0;
			m_handBoxOffY = 0;
			
			loadHandBox.contentLoaderInfo.addEventListener(Event.COMPLETE,OnLoadComplete);
		}
		
		public function addFun(owner:String,fun : Function,funType : String) : void
		{
			var funObj : Object = new Object;
			funObj["owner"] = owner;
			funObj["fun"] = fun;
			
			switch(funType)
			{
				case FUNTYPE_MOUSEMOVE:
					mouseMoveFunGroup.push(funObj);
					break;
				case FUNTYPE_MOUSECLICK:
					mouseClickFunGroup.push(funObj);
					break;
				case FUNTYPE_MOUSEDOWN:
					mouseDownFunGroup.push(funObj);
					break;
				case FUNTYPE_MOUSEUP:
					mouseUpFunGroup.push(funObj);
					break;
			}
		}
		
		public function removeFun(owner:String,fun : Function,funType : String) : void
		{
			switch(funType)
			{
				case FUNTYPE_MOUSEMOVE:
					for(var i:int=0;i<mouseMoveFunGroup.length;i++)
					{
						if(mouseMoveFunGroup[i]["owner"] == owner)
						{
							if(mouseMoveFunGroup[i]["fun"] == fun)
							{
								mouseMoveFunGroup.splice(i,1);
								break;
							}
						}
					}
					break;
			}
		}
		
		public function startMove() : void
		{
			if(!XG.stage.hasEventListener(MouseEvent.MOUSE_MOVE))
			{
				XG.stage.addEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove);
			}
		}
		
		public function stopMove() : void
		{
			XG.stage.removeEventListener(MouseEvent.MOUSE_MOVE,OnMouseMove);
		}
		
		public function OnMouseMove(evt : MouseEvent) : void
		{
			for(var i:int=0;i<mouseMoveFunGroup.length;i++)
			{
				mouseMoveFunGroup[i]["fun"](evt);
			}
			
			mouseMove();
		}
		
		private function OnLoadComplete(e:Event) : void
		{
			XG.showMouseHand(loadHandBox);
			loadHandBox.mouseEnabled = false;
			
			loadHandBox.x = XG.stage.mouseX - loadHandBox.width/2;
			loadHandBox.y = XG.stage.mouseY - loadHandBox.height;
		}
		
		public function mouseMove():void
		{
			if(loadHandBox.content)
			{
				loadHandBox.x = XG.stage.mouseX - loadHandBox.width/2;
				loadHandBox.y = XG.stage.mouseY - loadHandBox.height;
			}
			
			handBox.x = XG.mouseX + m_handBoxOffX;
			handBox.y = XG.mouseY + m_handBoxOffY;
		}
		
		public function setHandBoxOffset(offX : Number,offY : Number) : void
		{
			m_handBoxOffX = offX;
			m_handBoxOffY = offY;
		}
		
		public function destory() : void
		{
			handBox = 				null;
			
			loadHandBox.unloadAndStop();
			loadHandBox = 			null;
			
			mouseMoveFunGroup = 	null;
			mouseClickFunGroup = 	null;
			mouseDownFunGroup = 	null;
			mouseUpFunGroup = 		null;
			
			m_handBoxOffX = 		0;
			m_handBoxOffY = 		0;
			
			box = 					null;
		}
	}
}